<?php
	//error_reporting(0);
	set_time_limit(E_ALL);
	include 'simplehtmldom/simple_html_dom.php';
	include_once(dirname(__FILE__).'/dbutil.php');
	function saveRecord($table,$record,$db){
    	if(empty($table))
    		return false;
    	if(!is_array($record) || empty($record))
    		return false;
    	//$db = new DBUtil();
    	//$this->initDB();
    	$r = array();
    	foreach($record as $key=>$item){
    		$r[$db->escape($key)] = $db->escape($item);
    	}
    	$cols = implode("," , array_keys($r));
    	$values = implode("','" , $r);
    	$sql = "insert into " . $db->escape($table) . " (" . $cols . ") values ('" . $values . "')";
    	return $db->query($sql) ? $db->getlastid() : false;
    }
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	// 房天下
	$baseUrl = 'http://esf.yichun.fang.com';//需要修改
	$url = $baseUrl . '/housing/';
	//$url = 'http://esf.binzhou.fang.com/housing/__0_0_0_0_63_0_0_0/';
	$p = '';
	while(true){
		$recordList = array();
		// 西安房天下
		$listPage = file_get_html('compress.zlib://'.$url);
		//echo $listPage->innertext;exit;
		//$listPage = file_get_contents('compress.zlib://http://newhouse.xian.fang.com/house/dianshang/');
		//echo $listPage
		if(empty($listPage)){
			break;
		}
		$list = $listPage->find('div[class=list]');
		//var_dump($list);exit;
		if(!empty($list)){
			foreach($list as $listItem){
				//var_dump(mb_convert_encoding($listItem->innertext,'utf-8','gbk'));
				$tmp = $listItem->find('a[class=plotTit]');
				if(empty($tmp)){
					echo 1;exit;
					continue;
				}
				$name = trim(mb_convert_encoding($tmp[0]->innertext,'utf-8','gbk'));
				$tmp = $listItem->find('dd')[0]->find('p')[1];
				$address = getAddress($tmp->innertext);
				//echo $address;exit;
				$record = array();
				$record['house_name'] = $name ;
				$record['address'] = $address;
				//$record['city'] = 'fuzhou';
				//状态
				$record['status'] = 0;
				$recordList[] = $record;
				//break;
			}
		//	var_dump($recordList);exit;
			$db = new DBUtil();
			//var_dump($recordList);exit;
			foreach($recordList as $item){
				$h = $db->getOneRow("select id from t_house where house_name = '".$db->escape($item['house_name'])."' ");
				if(empty($h)){
					// 保存楼盘信息
					saveRecord('t_house',$item,$db);
				}
			}
			$db->close();
		}

		$nextPage = $listPage->find('div[class=fanye]')[0]->find('a[class=pageNow]')[0]->next_sibling();
		echo $nextPage->href;
		if(!empty($nextPage) ){
			$url = $baseUrl . $nextPage->href;
		}else{
			break;
		}
		//sleep(30);
		//var_dump( $url);exit;
	}
	echo 'finish';
	// 删除html
	function getAddress($value){
		preg_match('/.*<a.*?>(.*)<\/a>(.*)<a.*?>(.*)<\/a>(.*)/', $value, $matches);
		$address = '';
		for($i=1;$i<count($matches) - 1;$i++){
			$address .= trim($matches[$i]);
		}
		$address .= ' ' . trim(mb_convert_encoding($matches[$i],'utf-8','gbk'));
		return $address;
		/*return preg_replace('/<.*?>/','',$value);*/
	}
	// 删除全部空格
	function removeSpace($value){
		return preg_replace('/\s+/','',$value);
	}
?>